package com.attilax.net;

/**
 * @author MrBread
 * @date 2017骞�鏈�8鏃�
 * @time 涓嬪崍3:14:05
 * @project_name TestSocket
 * 鍔熻兘锛氭娴嬫湰鏈虹鍙ｆ槸鍚﹀凡缁忚浣跨敤鐢�
 */
 

import java.io.IOException;  
import java.net.InetAddress;  
import java.net.Socket;  
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.Test;

import com.attilax.util.ExUtil;  
@SuppressWarnings("all")
public class PortChk {  
    //start--end鏄墍瑕佹娴嬬殑绔彛鑼冨洿
    
    public static void main(String args[]){
    	System.out.println(System.currentTimeMillis());
    	
    	 long timeStamp = 1495777335060L;//直接是时间戳  13位  13bit
    	 timeStamp=9999999999999L;    //2286-11-21 01:46:39
    //	long timeStamp = System.currentTimeMillis();  //获取当前时间戳,也可以是你自已给的一个随机的或是别人给你的时间戳(一定是long型的数据)
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//这个是你要转成后的时间的格式
    	String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));   // 时间戳转换成时间
    	        System.out.println(sd);//打印出你要的时间
    	  int start=8443;
      
          System.out.println( PortChk.getNonUsePort(start));
      
      
    }
    public static int getNonUsePort(int start) {
        int end=start+1024;
    	  for(int i=start;i<=end;i++){
              
              if(isLocalPortUsing(i)){
            	  System.out.println(" posrt useed:"+i);
                 continue;
              }
              return i;
            
    	  }
		return end;
     
	}
	/**
     * 娴嬭瘯鏈満绔彛鏄惁琚娇鐢�
     * @param port
     * @return
     */
 
    public static boolean isLocalPortUsing(int port){  
        boolean flag = true;  
        try {
            //濡傛灉璇ョ鍙ｈ繕鍦ㄤ娇鐢ㄥ垯杩斿洖true,鍚﹀垯杩斿洖false,127.0.0.1浠ｈ〃鏈満
            flag = isPortUsing("127.0.0.1", port);  
        } catch (Exception e) {  
        	return false;
        }  
        return flag;  
    }  
    
    
    @Test
    public   void  test_isPortUsing ()
    {
    //	System.out.println(  PortChk. isPortUsing_local(22));
    	try {
			System.out.println(  PortChk. isPortUsing("10.0.0.10",22));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 
    }
    
    
    @Test
    public   void  test_isPortUsing_local()
    {
    //	System.out.println(  PortChk. isPortUsing_local(22));
    //	System.out.println(  PortChk. isPortUsing_local(22));
	 
    }
    
    public static boolean isPortUsing_local_retBool(int port){  
    	 
        
        //濡傛灉璇ョ鍙ｈ繕鍦ㄤ娇鐢ㄥ垯杩斿洖true,鍚﹀垯杩斿洖false,127.0.0.1浠ｈ〃鏈満
        try {
        	isPortUsing_local(port);
		 
		} catch (Exception e) {
			return false;
		}  
    
    return true;  
}  
    
    
    public static void isPortUsing_local(int port){  
 
         
            //濡傛灉璇ョ鍙ｈ繕鍦ㄤ娇鐢ㄥ垯杩斿洖true,鍚﹀垯杩斿洖false,127.0.0.1浠ｈ〃鏈満
            try {
				  isPortUsing("127.0.0.1", port);
			} catch (UnknownHostException e) {
				ExUtil.throwExV2(e);
			} catch (IOException e) {
				ExUtil.throwExV2(e);
			}  
        
       
    }  
    /*** 
     * 娴嬭瘯涓绘満Host鐨刾ort绔彛鏄惁琚娇鐢�
     * @param host 
     * @param port 
     * @throws IOException 
     */  
    public static boolean isPortUsing(String host,int port) throws IOException{  
       
        InetAddress Address = InetAddress.getByName(host);  
        
           
			Socket socket = new Socket(Address,port);  //寤虹珛涓�釜Socket杩炴帴
            
         
        return true;  
    }  
}  